package ooboo.gui;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Point;
import java.awt.Toolkit;
import java.util.Vector;

import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;

import ooboo.TweetThread;

public class MainDisplay extends JFrame
{
	private static final long serialVersionUID = 1L;
	
	private Vector<ThreadPanel> threads;
	private JScrollPane scrollPane;
	private Box tweetBox;
	private PostPanel postPanel;
	
	public MainDisplay()
	{
		threads = new Vector<ThreadPanel>();
		
		Container mainWindow = getContentPane();
		
		//mainWindow.setBackground(Color.GRAY);
		mainWindow.setLayout(new BorderLayout());
		{
			scrollPane = new JScrollPane();
			scrollPane.getViewport().setOpaque(false);
			//scrollPane.setBackground(Color.GRAY);
			scrollPane.setBorder(null);
			scrollPane.setOpaque(false);
			scrollPane.setViewportBorder(null);
			scrollPane.getVerticalScrollBar().setUnitIncrement(16);
			
			mainWindow.add(scrollPane);
			
			postPanel = new PostPanel();
			mainWindow.add(postPanel,BorderLayout.SOUTH);			
		}
				
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setIconImage(new ImageIcon("images/billy128.png").getImage());
		setSize(400,(int)(Toolkit.getDefaultToolkit().getScreenSize().height * 0.8));
		setTitle("ooboo");
		setVisible(true);
	}
	
	public PostPanel getPostPanel()
	{
		return postPanel;
	}
	
	/*public void newTweet(String tweet)
	{
		Box temp = Box.createVerticalBox();
		
		TweetThread newThread = new TweetThread();
		
		newThread.add(tweet);
		
		threads.add(newThread);
		
		for(TweetThread thread:threads)
		{
			temp.add(thread);
		}
		
		tweetBox = temp;
		scrollPane.setViewportView(tweetBox);
	}*/
	
	public void setThreads(TweetThread[] threads)
	{
		final Point p = scrollPane.getViewport().getViewPosition();
		
		tweetBox = Box.createVerticalBox();
		
		for (int i = threads.length - 1; i >= 0; i--){
			tweetBox.add(new ThreadPanel(threads[i]));
		}
		
		/*for(List<StoredTweet> thread:threads)
		{
			tweetBox.add(new ThreadPanel(thread));
		}*/
		
		scrollPane.setViewportView(tweetBox);
		
		SwingUtilities.invokeLater(new Runnable() {
	         public void run() {
	            scrollPane.getViewport().setViewPosition(p);
	         }
	      });

		/*try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		scrollPane.getViewport().setViewPosition(p);/**/

	}
}
